{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 找零钱问题——贪心算法",
   "id": "a3308224e3343e22"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-01T07:17:10.499764Z",
     "start_time": "2025-03-01T07:17:06.562631Z"
    }
   },
   "source": [
    "def change():\n",
    "    d=[0.01,0.02,0.05,0.1,0.2,0.5,1]\n",
    "    while True:\n",
    "        try:\n",
    "            sum = float(input(\"请输入需要找的零钱：\"))\n",
    "            if sum<=0:\n",
    "                print(\"您不是一个正数！\")\n",
    "            else:\n",
    "                break\n",
    "        except:\n",
    "            print(\"输入异常，请重新输入！\")\n",
    "    i=len(d)-1\n",
    "    while i>=0:\n",
    "        if sum>=d[i]:\n",
    "            n=int(sum/d[i])\n",
    "            sum=float(\"{:.2f}\".format(sum-n*d[i]))\n",
    "            print(\"找了{}个{}元硬币\".format(n,d[i]))\n",
    "        i-=1\n",
    "change()\n",
    "        \n",
    "                \n",
    "            "
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找了2个1元硬币\n",
      "找了1个0.5元硬币\n",
      "找了2个0.2元硬币\n",
      "找了1个0.02元硬币\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
